precision mediump float;
uniform sampler2D uSampler;
varying vec2 vTexCoord;
uniform vec2 uImageSize;

uniform highp vec2 uCenterLeftEye;
uniform highp vec2 uCenterRightEye;

uniform highp vec2 uBeginLeftEye;
uniform highp vec2 uEndLeftEye;
uniform highp vec2 uBeginRightEye;
uniform highp vec2 uEndRightEye;

uniform vec2 uLeftCheekOne;
uniform vec2 uLeftCheekTwo;
uniform vec2 uRightCheekOne;
uniform vec2 uRightCheekTwo;

uniform vec2 uNoseOne;
uniform vec2 uNoseTwo;
//curPos 是纹理坐标
//centerEyePos 是图片坐标
vec2 newCoord(vec2 curPos, vec2 centerEyePos, float rMax, float alpha) {
    vec2 result = curPos;
    //纹理坐标转图片坐标
    vec2 imgCurPos = curPos * uImageSize;

    float r = distance(imgCurPos, centerEyePos);
    if( r < rMax){
        //k = 1 - power((r / r_max  - 1), 2) * a
        float k = 1.0 - pow(r/rMax - 1.0, 2.0) * alpha;
        //x_d = k * (x_c - x_0) + x_0
        //y_d = k * (y_c - y_0) + y_0
        result = k * (imgCurPos - centerEyePos) + centerEyePos;
        //图片坐标转纹理坐标
        result = result / uImageSize;
    }

    return result;
}


//curPos 当前像素的位置
//originPos 当前圆的圆心
// directPos 目标点圆心
//移动系数 a
vec2 slimmingFace(vec2 curPos, vec2 originPos, vec2 directPos, float alpha){
    vec2 result;
    result = curPos * uImageSize;

    vec2 radius = (directPos - originPos) * 0.5;
    vec2 targetPos = originPos + radius;
    float rMax = distance(targetPos, originPos);
    float r = distance(result, originPos);
    if(r < rMax){
        float k = pow(1.0 - r / rMax, 2.0) * alpha;
        result = result - k * radius;
    }
    result = result / uImageSize;

    return result;
}

void main() {
    float rMaxLeft = distance(uBeginLeftEye, uEndLeftEye) / 2.0;
    float rMaxRight = distance(uBeginRightEye, uEndRightEye) / 2.0;

    highp float eyeWidth = uEndRightEye.x - uBeginLeftEye.x;
    //a = eyeWidth * power(512, 2) / (width * height * 100)
    highp float a = eyeWidth * 512.0 * 512.0 / (uImageSize.x * uImageSize.y * 100.0);

    vec2 nc = newCoord(vTexCoord, uCenterLeftEye, rMaxLeft, a);
    nc = newCoord(nc, uCenterRightEye, rMaxRight, a);

    //-5.0，5.0 图片坐标 左脸左边 a的值越大瘦脸越明显0.2 0.1
    vec2 np = slimmingFace(nc, uLeftCheekOne + vec2(-5.0, 5.0), uNoseOne, 0.1);
    np = slimmingFace(np, uLeftCheekTwo + vec2(-5.0, 5.0), uNoseTwo, 0.1);
    //5.0，5.0 图片坐标 右脸右边
    np = slimmingFace(np, uRightCheekOne + vec2(5.0, 5.0), uNoseOne, 0.1);
    np = slimmingFace(np, uRightCheekTwo + vec2(5.0, 5.0), uNoseTwo, 0.1);

    gl_FragColor = texture2D(uSampler, np);
}
